﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace DAO
{
    public class DataProvider
    {
        //public const string cnStr = "Persist Security Info=False;Trusted_Connection=True;database=QLDATMONAN2_Loi;server=(local)";
        //public const string cnStr_Sua = "Persist Security Info=False;Trusted_Connection=True;database=QLDATMONAN2_SuaLoi;server=(local)";
        public const string cnStr_Sua = "Data Source=localhost;Initial Catalog=QL_DATMONAN_FIXED;user=sa;password=123456";
        public const string cnStr = "Data Source=localhost;Initial Catalog=QL_DATMONAN_ERROR;user=sa;password=123456";
        SqlConnection cn;
        //SqlConnection cn_Sua;
        public DataProvider(int loai)
        {
            if (loai == 0)
            {
                cn = new SqlConnection(cnStr);
            }
            else
            {
                cn = new SqlConnection(cnStr_Sua);
            }
        }
        public static SqlConnection ConnectDB(int loai)
        {
            string connectionString;
            if (loai == 0)
            {
                connectionString = @"Data Source=localhost;Initial Catalog=QL_DATMONAN_ERROR;user=sa;password=123456";
            }
            else
            {
                connectionString = @"Data Source=localhost;Initial Catalog=QL_DATMONAN_FIXED;user=sa;password=123456";
            }
            SqlConnection conn;
            conn = new SqlConnection(connectionString);
            conn.Open();
            return conn;
        }
        public void Connect()
        {
            cn.Open();
            // cn_Sua.Open();
        }

        public void Disconnect()
        {
            cn.Close();
            // cn_Sua.Close();
        }

        public DataSet ExecuteDataTable(string sql, CommandType commandType, params object[] pars)
        {
            DataSet dst = new DataSet();
            try
            {

                cn.Open();
                SqlCommand com = new SqlCommand(sql, cn);
                com.CommandType = commandType;
                for (int i = 0; i < pars.Length; i += 2)
                {
                    SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    com.Parameters.Add(par);
                }
                SqlDataAdapter dad = new SqlDataAdapter(com);
                dad.Fill(dst);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            cn.Close();
            return dst;
        }

        public DataTable ExecuteQuery(string sql, CommandType commandType, params object[] pars)
        {
            DataTable dt = new DataTable();
            try
            {
                cn.Open();
                SqlCommand com = new SqlCommand(sql, cn);
                com.CommandType = commandType;
                for (int i = 0; i < pars.Length; i += 2)
                {
                    SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    com.Parameters.Add(par);
                }
                SqlDataAdapter dad = new SqlDataAdapter(com);
                dad.Fill(dt);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            cn.Close();
            return dt;
        }

        public void ExecuteNonQuery(string sql, CommandType commandType, params object[] pars)
        {
            try
            {
                cn.Open();
                SqlCommand com = new SqlCommand(sql, cn);
                com.CommandType = commandType;

                for (int i = 0; i < pars.Length; i += 2)
                {
                    SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    SqlParameter par_sua = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    com.Parameters.Add(par);

                }
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            cn.Close();
        }
        public Object ExecuteScalar(string sql, CommandType commandType, params object[] pars)
        {
            Object obj = new Object();
            try
            {
                SqlCommand com = new SqlCommand(sql, cn);
                com.CommandType = commandType;
                for (int i = 0; i < pars.Length; i += 2)
                {
                    SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    com.Parameters.Add(par);
                }
                obj = com.ExecuteScalar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return obj;
        }
        public DataSet ExecuteDataSet(string sql, CommandType commandType, params object[] pars)
        {
            DataSet dst = new DataSet();
            try
            {
                SqlCommand com = new SqlCommand(sql, cn);
                com.CommandType = commandType;
                for (int i = 0; i < pars.Length; i += 2)
                {
                    SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                    com.Parameters.Add(par);
                }
                SqlDataAdapter dad = new SqlDataAdapter(com);
                dad.Fill(dst);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return dst;
        }
    }

}
